--
-- Author: shunguo.chen
-- Date: 2018-04-12 14:10:32
--
-- ChessTipRender chess格子tip
--

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.ChessTipNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

-- data = msg
function ClassRef:init(data)
    self.super.init(self, data)

    self._textSize = self._ui.Text:getContentSize()
    self._panelSize = self._ui.Panel:getContentSize()
    self._timeline = self:getTimeline()

    display.nodeRemoveForNotHit(self._ui.Panel, function()
        if self:isVisible() then
            self:hide()
        end
    end)

    self:refresh()
end

function ClassRef:show()
    self:setVisible(true)
    self._timeline:gotoFrameAndPlay(0, false)
end

function ClassRef:hide()
    self:setVisible(false)
    self._timeline:pause()
end

function ClassRef:refresh()
    if self._data then
        self._ui.Text:setTextAreaSize(cc.size(self._textSize.width, 0))
        self._ui.Text:setString(self._data)

        local size = self._ui.Text:getVirtualRendererSize()
        local dis = size.height - self._textSize.height
        if dis > 0 then
            self._ui.Panel:setContentSize(cc.size(self._panelSize.width, self._panelSize.height + dis))
        else
            self._ui.Panel:setContentSize(self._panelSize)
        end
        ccui.Helper:doLayout(self._ui.Panel)
    end
end

return ClassRef
